package cn.com.captureNetPacket.controller;

import cn.com.captureNetPacket.util.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.com.captureNetPacket.model.ConfigInfo;
import cn.com.captureNetPacket.model.ParamCondition;
import cn.com.captureNetPacket.model.WebResult;
import cn.com.captureNetPacket.service.ConfigInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/if/capture/config")
@Api(value = "ConfigInfoController", tags = { "ConfigInfoController" }, description = "CONFIGINFO相关")
public class ConfigInfoController {

	@Autowired
	private ConfigInfoService configInfoService;
	@Autowired
	private ObjectMapper mapper;

	@GetMapping("/getAll")
	@ApiOperation("查询CONFIGINFO表中所有数据")
	public Object getAllConfigInfo() throws JsonProcessingException {
		WebResult ws = configInfoService.searchAll();
		log.debug("---返回数据:---->{}", mapper.writeValueAsString(ws));
		return ws;
	}

	@GetMapping("/getAllByPaging/{currentPage}/{size}")
	@ApiOperation("分页查询CONFIGINFO表中所有数据")
	public Object getAllConfigInfoByPaging(@PathVariable(name = "currentPage") int currentPage,
			@PathVariable(name = "size") int size) throws JsonProcessingException {
		WebResult ws = configInfoService.searchByPaging(currentPage, size);
		log.debug("---返回数据:---->{}", mapper.writeValueAsString(ws));
		return ws;
	}

	@PostMapping("/addConfigInfo")
	@ApiOperation("添加数据到CONFIGINFO表")
	public Object addConfigInfo(@RequestBody ConfigInfo configInfo) throws JsonProcessingException {
		log.debug("---上送参数为:{}-----", configInfo);
		WebResult ws = configInfoService.create(configInfo);
		log.debug("---返回数据:---->{}", mapper.writeValueAsString(ws));
		return ws;
	}

	@DeleteMapping("/removeConfigInfo")
	@ApiOperation("根据id删除CONFIGINFO表中数据")
	public Object removeConfigInfo(@RequestBody ConfigInfo configInfo) throws JsonProcessingException {
		log.debug("---上送参数为:{}-----", configInfo);
		WebResult ws = configInfoService.remove(configInfo);
		log.debug("---返回数据:---->{}", mapper.writeValueAsString(ws));
		return ws;
	}

	@PatchMapping("/modifyConfigInfo")
	@ApiOperation("根据id修改CONFIGINFO表中数据")
	public Object modifyConfigInfo(@RequestBody ConfigInfo configInfo) throws JsonProcessingException {
		log.debug("---上送参数为:{}-----", configInfo);
		WebResult ws = configInfoService.modify(configInfo);
		log.debug("---返回数据:---->{}", mapper.writeValueAsString(ws));
		return ws;
	}

	@PostMapping("/getConfigInfoByConditionAndPaging")
	@ApiOperation("分页+条件查询CONFIGINFO表中数据")
	public Object getConfigInfoByConditionAndPaging(@RequestBody ParamCondition<ConfigInfo> paramCondition)
			throws JsonProcessingException {
		log.debug("---上送参数为:{}-----", mapper.writeValueAsString(paramCondition));
		WebResult ws = configInfoService.searchByCondition(paramCondition.getCurrentPage(), paramCondition.getSize(),
				paramCondition);
		log.debug("---返回数据:---->{}", mapper.writeValueAsString(ws));
		return ws;
	}


	@PostMapping("/publishMessage/{type}")
	@ApiOperation("发布消息。")
	public Object publishMessage(@PathVariable(name = "type") String type,
										   @RequestBody Map content) throws JsonProcessingException {
		WebResult ws = new WebResult();
		int result=RedisConfig.publishMessage(type,content);
		ws.setErrorMsg("发布成功《"+content+"》完成");
		ws.setRetcode(result+"");
		return ws;
	}

}
